Grokking JavaScript Fundamentals
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who should take this course?
Course Overview
Introduction to JavaScript
JavaScript Basics
JavaScript - Hello World
JavaScript - Comments
Quiz
JavaScript Variables and Operators
JavaScript - Variables
JavaScript - Operators
Quiz
JavaScript Conditionals
Introduction to Conditional Statements in JavaScript
JavaScript - If...Else
JavaScript - Switch Case
Quiz
JavaScript Objects
Introduction to Objects in JavaScript
JavaScript - Number
JavaScript - Boolean
JavaScript - Math
JavaScript - Date
Quiz
JavaScript - Functions
Introductions to Function in JavaScript
JavaScript - Variable Scope
JavaScript - Default Parameters
JavaScript - Arrow Functions
Quiz
JavaScript - String and Arrays
Introduction to Strings in JavaScript
JavaScript - String Methods
Introduction to Array in JavaScript
JavaScript - Array Methods
Quiz
JavaScript Loops
Introduction to loops in JavaScript
JavaScript - for loop
JavaScript - while loop
JavaScript - do-while loop
JavaScript - for-in loop
JavaScript - for-of loop
JavaScript - Loop Control
Quiz
JavaScript - Maps
JavaScript - Sets
Quiz
Where to Go from Here
JavaScript - Loop Control

In JavaScript, controlling the flow of loops is crucial for managing how and when loops start, execute, and terminate. This control is achieved through various statements, including label, continue, and break statements. Each serves a unique purpose in enhancing the flexibility and efficiency of loop operations.

Label Statement

A label provides a statement with an identifier that allows you to refer to it elsewhere in your code, particularly with break and continue statements. Labels are useful for breaking out of nested loops or continuing a loop iteration at a specific point in nested loops.

Syntax

Javascript
. . . .

Parameters

  • labelName: Any valid JavaScript identifier.
  • statement: Any statement to label.

Example

In this example, we use a label with a break statement to exit from a nested loop.

Javascript

. . . .

This code iterates over a pair of nested loops, but when both i and j equal 1, it breaks out of the outer loop entirely, thanks to the outerLoop label.

Continue Statement

The continue statement skips the current iteration of a loop and continues with the next iteration. It's particularly useful when a specific condition within the loop doesn't require the execution of the remaining loop body.

Syntax

Javascript
. . . .
  • Optionally, you can specify a label to continue the next iteration of the labeled loop.

Example

Here's how you can use continue to skip certain iterations in a loop.

Javascript

. . . .

This loop prints the numbers 0 to 4, except for 2, because the continue statement skips the loop's remaining body when i is 2.

Break Statement

The break statement terminates the loop immediately when executed. It's used to exit from a loop before it has iterated over all its elements, usually when a certain condition is met.

Syntax

Javascript
. . . .
  • Just like with continue, a label can be specified to break out of a specific loop in nested loops.

Beginner Friendly Example

This example demonstrates using break to exit a loop early.

Javascript

. . . .

The loop will print the numbers 0 to 2. When i equals 3, the break statement is executed, terminating the loop before all iterations are complete.

Through the use of label, continue, and break statements, JavaScript provides powerful tools for controlling loop execution. These control statements enhance the language's flexibility, allowing for more precise and efficient looping constructs.

JavaScript - for-of loop
Quiz
Mark as Completed

On this page

Label Statement

Syntax

Parameters

Example

Continue Statement

Syntax

Example

Break Statement

Syntax

Beginner Friendly Example